import {request} from '../../request/index.js';
let app = getApp();
let search = require('../../components/search/search');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        checkid:0,
        userid:"",
        user_list:[]
    },
    // 初始化数据
    getLoadData(){
        let this_ = this;
        wx.getStorage({
          key: 'userState',
          success:(res)=>{
            request({url:app.data.url+"users/getUser",data:{code:res.data}}).then(result=>{
                return new Promise((resolve,reject)=>{
                    resolve(result.data.message[0].id);
                })
            }).then(result=>{
                request({url:app.data.url+"users/getMyFriends",data:{userid:result}}).then(users=>{
                    this_.setData({userid:result,user_list:users.data.message});
                })
            })
          }
        })
        
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if(options.checkid){
            this.setData({checkid:options.checkid})
        }
        this.getLoadData();
    },
    // 子组件回调事件
    setId(e){
        this.setData({checkid:e.detail.switch_index});
        console.log(search);
    },
    getParams(e){
        request({url:app.data.url+"users/addOtherUsers",data:{userid:this.data.userid,otheruserid:e.detail.params.userid}}).then(res=>{
            wx.showToast({
              title: '添加成功',
              icon:"success",
              success:()=>{
                //   成功之后初始化数据
                this.getLoadData();
              }
            })
        })
    },
    getKeyWord(e){
        let this_ = this;
        let keyword = e.detail.value.trim();
        if(keyword==""){
            this.getLoadData();
        }else{
            request({url:app.data.url+"users/getUserByName",data:{checkid:this.data.checkid,userid:this.data.userid,keyword}}).then(res=>{
                this.data.checkid==0?this_.setData({user_list:{unMyfriendsList:res.data.message}}):this_.setData({user_list:{myfriendsList:res.data.message}});
            })
        }
    },
    getSend(e){
        wx.navigateTo({
            url: '/pages/message_chat/message_chat?uid='+this.data.userid+'&oid='+e.detail.params.userid+'&nickname='+e.currentTarget.dataset.nickname
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        app.setTheme(app.data.themeid)
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})